package com.kdzy.lshy.common.service;

import com.alibaba.fastjson.JSONObject;
import com.kdzy.lshy.common.common.XunCheng;
import lombok.experimental.var;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: hanrui
 * @Date: 2018/6/19
 * @Description:
 */

@Service(value = "idCheckService")
public class IDCheckService {

    @Autowired
    private IDCardService idcardService;

    @Autowired
    private XunCheng xunCheng;

    /**
     * 获取身份证信息
     * <p>
     * 参数: base64string_id ,base64string_photo
     *
     * @return
     */

    public Object checkInfo( String base64string_id,String base64string_photo, HttpServletRequest request) {


        JSONObject person =(JSONObject) invoke_getinfo(base64string_id);

        String realName = person.getString("name");

        String cardNo = person.getString("num");

        return invoke_check(realName,cardNo,base64string_photo);

    }


    public Object invoke_getinfo(String bas64String){

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(xunCheng.getInterface_ocr());
        List<NameValuePair> kvList = new ArrayList<NameValuePair>();
        kvList.add(new BasicNameValuePair("key", xunCheng.getApiKey()));
        kvList.add(new BasicNameValuePair("bas64String", bas64String));
        kvList.add(new BasicNameValuePair("side", "1"));
        StringEntity entity = null;
        JSONObject id_json = new JSONObject();

        try {
            entity = new UrlEncodedFormEntity(kvList, "utf-8");
            post.setEntity(entity);
            CloseableHttpResponse response = httpClient.execute(post);
            String string = EntityUtils.toString(response.getEntity());
            id_json = JSONObject.parseObject(string);
            response.close();
            httpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return id_json;
    }

    public Object invoke_check(String realName,String cardNo,String bas64String){

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(xunCheng.getInterface_ocr());
        List<NameValuePair> kvList = new ArrayList<NameValuePair>();
        kvList.add(new BasicNameValuePair("key", xunCheng.getApiKey()));
        kvList.add(new BasicNameValuePair("realName", realName));
        kvList.add(new BasicNameValuePair("cardNo", cardNo));
        kvList.add(new BasicNameValuePair("img", bas64String));
        StringEntity entity = null;
        JSONObject check = new JSONObject();

        try {
            entity = new UrlEncodedFormEntity(kvList, "utf-8");
            post.setEntity(entity);
            CloseableHttpResponse response = httpClient.execute(post);
            String string = EntityUtils.toString(response.getEntity());
            check = JSONObject.parseObject(string);
            response.close();
            httpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return check;
    }

}
